package android.slc.popup.impl;

import android.slc.popup.SlcPopup2;
import android.slc.popup.fast.ShadowPopupWindow;

import java.lang.ref.WeakReference;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2022/04/07 下午 7:05
 */
public class ShadowPopupOperateImpl extends BaseOperateImpl<ShadowPopupWindow> implements SlcPopup2.ShadowPopupOperate {
    private ShadowPopupWindow mShadowPopupWindow;
    private final OnOperateDismissListener mOnOperateDismissListener;
    private final OnOperateShowListener mOnOperateShowListener;
    private ShadowPopupWindow.OnDismissListener mOnDismissListener;
    private ShadowPopupWindow.OnShowListener mOnShowListener;

    public ShadowPopupOperateImpl() {
        super();
        this.mOnOperateDismissListener = new OnOperateDismissListener(this);
        this.mOnOperateShowListener = new OnOperateShowListener();
    }

    @Override
    public SlcPopup2.ShadowPopupOperate setOnDismissListener(ShadowPopupWindow.OnDismissListener onDismissListener) {
        this.mOnDismissListener = onDismissListener;
        return this;
    }

    @Override
    public SlcPopup2.ShadowPopupOperate setOnShowListener(ShadowPopupWindow.OnShowListener onShowListener) {
        this.mOnShowListener = onShowListener;
        return this;
    }

    @Override
    public ShadowPopupWindow getListPopupWindow() {
        return mShadowPopupWindow;
    }


    @Override
    public void dismiss() {
        mShadowPopupWindow.dismiss();
    }

    @Override
    public void show(ShadowPopupWindow shadowPopupWindow) {
        if (SlcPopup2.existOperateByKey(getKey())) {
            return;
        }
        this.mOnOperateDismissListener.setKey(getKey());
        this.mOnOperateDismissListener.setOnDismissListener(this.mOnDismissListener);
        this.mOnOperateShowListener.setOnShowListener(this.mOnShowListener);
        this.mShadowPopupWindow = shadowPopupWindow;
        this.mShadowPopupWindow.setOnDismissListener(this.mOnOperateDismissListener);
        this.mShadowPopupWindow.setOnShowListener(this.mOnOperateShowListener);
        this.mShadowPopupWindow.show();
        SlcPopup2.addOperate(getKey(), this);
    }

    private static class OnOperateDismissListener implements ShadowPopupWindow.OnDismissListener {
        private ShadowPopupOperateImpl baseOperate;
        private WeakReference<ShadowPopupWindow.OnDismissListener> onDismissListenerWeakReference;
        private String key;

        private OnOperateDismissListener(ShadowPopupOperateImpl baseOperate) {
            this.baseOperate = baseOperate;
        }

        private void setOnDismissListener(ShadowPopupWindow.OnDismissListener onDismissListener) {
            this.onDismissListenerWeakReference = new WeakReference<>(onDismissListener);
        }

        public void setKey(String key) {
            this.key = key;
        }

        @Override
        public void onDismiss() {
            ShadowPopupWindow.OnDismissListener onDismissListener = this.onDismissListenerWeakReference.get();
            this.baseOperate.mShadowPopupWindow = null;
            this.baseOperate = null;
            SlcPopup2.removeOperate(this.key);
            if (onDismissListener != null) {
                onDismissListener.onDismiss();
            }
        }
    }

    private static class OnOperateShowListener implements ShadowPopupWindow.OnShowListener {
        private WeakReference<ShadowPopupWindow.OnShowListener> onShowListenerWeakReference;

        private OnOperateShowListener() {
        }

        private void setOnShowListener(ShadowPopupWindow.OnShowListener onShowListener) {
            this.onShowListenerWeakReference = new WeakReference<>(onShowListener);
        }

        @Override
        public void onShow() {
            ShadowPopupWindow.OnShowListener onShowListener = onShowListenerWeakReference.get();
            if (onShowListener != null) {
                onShowListener.onShow();
            }
        }
    }
}
